const url = new URL(window.location.href);        // 获取当前页面的 URL
const params = new URLSearchParams(url.search);   // 获取查询字符串部分

// 获取单个参数
const id = params.get('id');   // id

// 获取 token
let token = localStorage.getItem('token');
if (!token) {
    // 使用 a标签 进行跳转
    let a = document.createElement('a');
    a.href = '/login.html';
    a.click();
}

// 发起请求
fetch(`http://127.0.0.1:3000/store/goods?id=${ id }`, {
    method: 'GET',
    headers: {
        'Authorization': token
    }
}).then((response) => {
    return response.json()
}).then((data) => {
    console.log('Success:', data);
    let goods_name = document.querySelector('.goods_name');
    let goods_img = document.querySelector('.goods_img');
    goods_name.innerText = data.data.name;
    goods_img.src = `http://127.0.0.1:3000${data.data.imgUrl}`;
}).catch(error => {
    console.error('Error:', error);
    alert('获取商品错误！');
});